<template>
	<view>
		<view class="setting-list" id="setting-list">

			<view class="text-1" style="margin-top: 8px;">当前选择</view>

			<view style="display: flex;" id='location'>
				<view class="cityText">
					<view class="text-3" style="margin-right: 10px;">
						{{city}}
					</view>
				</view>
			</view>
			<scroll-view scroll-y="true" style="height: calc(100% - 64px - 62px - 42px); top: 72px;position: absolute; "
				id="scroll-view" class="grid">
				<view class="text-1" style="margin-top: 8px;">请选择区</view>
				<block v-for="(item, index) in administrativeRegionList" :key="index">
					<view class="item-view" @click="selectAdministrativeRegion(item)">
						<view class="text-2" v-if="item.clicked" style="color: #EA5404;">{{item.name}}</view>
						<view class="text-2" v-else>{{item.name}}</view>
					</view>
					<view class="line"></view>




				</block>

			</scroll-view>
			<view id="saveBtn" class="saveBtn_no" @click="define_no" v-if="AdministrativeRegion==''">确定</view>
			<view id="saveBtn" class="saveBtn" @click="define" v-else>确定</view>

		</view>
	</view>
</template>

<script>
	const app = getApp();
	const dialog = require("../../../utils/dialog.js");
	const util = require("../../../utils/util.js");
	const network = require("../../../manager/network-manager.js");
	import indexAddress from "../../../components/t-index-address/t-index-address";
	// import AddressList from "../../../components/t-index-address/AddressData.json";
	export default {
		data() {
			return {
				province: '',
				city: '',
				provinceList: [],
				cityList: [],
				administrativeRegionList: [],
				cityCopy: '',
				AdministrativeRegion: ''
			}
		},

		onLoad(options) {
			console.log(options, 'options');
			this.province = options.province;
			this.city = options.city;

			// this.getAreaList(this.city)
		},
		onShow: function() {
			this.getAddressJson()

		},
		components: {
			indexAddress
		},
		methods: {
			getAddressJson: function() {
				var uri = app.globalData.config.interfaces.URL_AREA_GETADDRESSJSON;
				network.get(uri, '', 'json').then(res => {
					if (res.code == 200) {
						this.provinceList = res.data;
						this.initAddressList()
					} else {
						uni.showToast({
							title: res.message,
							duration: 3000,
							icon: 'none'
						})
					}
				}).catch(res => {
					uni.showToast({
						title: res.errMsg,
						duration: 3000,
						icon: 'none'
					})
					console.error(res);
				});


			},
			initAddressList() {
				for (var i = 0; i < this.provinceList.length; i++) {
					if (this.province == this.provinceList[i].name) {
						this.cityList = this.provinceList[i].children
					}
				}
				this.province.substring(this.province.length - 1)
				if (this.province.substring(this.province.length - 1) == '市') {
					this.administrativeRegionList = this.cityList[0].children
				} else {
					for (var j = 0; j < this.cityList.length; j++) {
						if (this.city == this.cityList[j].name.split("市").join("")) {
							this.cityCopy = this.cityList[j].name
							this.administrativeRegionList = this.cityList[j].children
						}
					}
				}
			},
			selectAdministrativeRegion: function(item) {
				this.AdministrativeRegion = item.name
				for (var i = 0; i < this.administrativeRegionList.length; i++) {
					if (this.administrativeRegionList[i].id == item.id) {
						this.administrativeRegionList[i].clicked = true
					} else {
						this.administrativeRegionList[i].clicked = false
					}
				}

			},
			define_no: function() {

				uni.showToast({
					title: "请选择行政区",
					duration: 3000,
					icon: 'none'
				})
			},
			define: function() {
				let pages = getCurrentPages(); //获取跳转的所有页面
				let nowPage = pages[pages.length - 1]; //当前页
				let prevPage = pages[pages.length - 3]; //上2页
				prevPage.$vm //上一页的this
				var area = ''
				if (this.province.substring(this.province.length - 1) == '市') {
					area = this.province + " " + this.AdministrativeRegion
				} else {
					area = this.province + " " + this.cityCopy + " " + this.AdministrativeRegion
				}

				prevPage.$vm.area = area
				prevPage.$vm.province = this.province
				prevPage.$vm.city = this.cityCopy
				prevPage.$vm.addressArea = this.AdministrativeRegion
				uni.navigateBack({
					delta: 2
				}); //这个是返回上级第一个页面， delta等于2的时候跳过上个页面返回再上一个页面

			}


		},

	}
</script>

<style>
	@import "./selectAdministrativeRegion.css";
</style>
